﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
using SerialPortHelperLib;

namespace XChipTool
{
    public partial class XChip_SCA : DevExpress.XtraEditors.XtraUserControl
    {
        public SerialPort sPortTemp;
        private bool isRead = false;

        public XChip_SCA()
        {
            InitializeComponent();
        }

        private void XChip_SCA_Load(object sender, EventArgs e)
        {
            //scaTrackBar1.SetScrollBarValue(60, 20);
            xChip_SCA_Area1.sPortTemp = sPortTemp;
            xChip_SCA_Area2.sPortTemp = sPortTemp;
            xChip_SCA_Area3.sPortTemp = sPortTemp;

            xChip_SCA_Area1.GetClick += _GetClick;
            xChip_SCA_Area2.GetClick += _GetClick;
            xChip_SCA_Area3.GetClick += _GetClick;
        }

       

        private void xtraTabControl1_Click(object sender, EventArgs e)
        {
            xChip_SCA_Area1.sPortTemp = sPortTemp;
            xChip_SCA_Area2.sPortTemp = sPortTemp;
            xChip_SCA_Area3.sPortTemp = sPortTemp;          
        }
        private void RGB_Protect_ShowHide(bool isbool)
        {
            r_red_u0.Enabled = isbool;
            r_red_u1.Enabled = isbool;
            r_red_v.Enabled = isbool;
            r_blue_u.Enabled = isbool;
            r_blue_v0.Enabled = isbool;
            r_blue_v1.Enabled = isbool;
            r_green_u.Enabled = isbool;
            r_green_v.Enabled = isbool;
        }
        private void _GetClick()
        {
            isRead = true;
            //if (SerialSend.ReadRegister(0x80301712, sPortTemp) == 0x01)
            if (GetSCARegister("RGB_Protect_En")==0x01)
            {
                RGB_Protect_En.Checked = true;
                RGB_Protect_ShowHide(true);
                r_red_v.Text = GetSCARegister("r_red_v").ToString("X2");//SerialSend.ReadRegister(0x80301713, sPortTemp).ToString("X2");
                r_red_u0.Text = GetSCARegister("r_red_u0").ToString("X2");//SerialSend.ReadRegister(0x80301714, sPortTemp).ToString("X2");
                r_red_u1.Text = GetSCARegister("r_red_u1").ToString("X2");//SerialSend.ReadRegister(0x80301715, sPortTemp).ToString("X2");
                r_blue_u.Text = GetSCARegister("r_blue_u").ToString("X2");//SerialSend.ReadRegister(0x80301716, sPortTemp).ToString("X2");
                r_blue_v0.Text = GetSCARegister("r_blue_v0").ToString("X2");//SerialSend.ReadRegister(0x80301717, sPortTemp).ToString("X2");
                r_blue_v1.Text = GetSCARegister("r_blue_v1").ToString("X2");//SerialSend.ReadRegister(0x80301718, sPortTemp).ToString("X2");
                r_green_u.Text = GetSCARegister("r_green_u").ToString("X2");// SerialSend.ReadRegister(0x80301719, sPortTemp).ToString("X2");
                r_green_v.Text = GetSCARegister("r_green_v").ToString("X2");//SerialSend.ReadRegister(0x8030171a, sPortTemp).ToString("X2");

            }
            else
            {
                RGB_Protect_En.Checked = false;
                RGB_Protect_ShowHide(false);
            }
            isRead = false;
        }

        private void RGB_Protect_En_CheckedChanged(object sender, EventArgs e)
        {
            if (RGB_Protect_En.Checked == true)
            {
                if (!isRead)
                    SetScaRegister("RGB_Protect_En", 0x01);
                RGB_Protect_ShowHide(true);
            }
            else {
                if (!isRead)
                    SetScaRegister("RGB_Protect_En", 0x00);
                RGB_Protect_ShowHide(false);
            }
        }
        private long GetSCARegister(string nameType)
        {
            if (XChipHelp.XChipSca.Where(c => c.NameType == nameType).Count() > 0)
                return SerialSend.ReadRegister(Convert.ToUInt32(XChipHelp.XChipSca.Where(c => c.NameType == nameType).First().Address, 16), sPortTemp);
            else { DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1; }
        }
        private void SetScaRegister(string nameType, ulong add)
        {
            if (!sPortTemp.IsOpen)//串口检测
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("串口未打开");
                return;
            }
            if (isRead) return;
            if (XChipHelp.XChipSca.Where(c => c.NameType == nameType).Count() > 0)
            {
                SerialSend.WriteRegister(Convert.ToUInt32(XChipHelp.XChipSca.Where(c => c.NameType == nameType).First().Address, 16), add, sPortTemp);
            }
        }

        private void r_red_u0_Leave(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (isRead) return;
            var txtTemp = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtTemp.Text)) return;
            switch ((txtTemp.Tag).ToString())
            {
                case "0":
                    SetScaRegister("r_red_u0", Convert.ToUInt32(txtTemp.Text, 16));//SerialSend.WriteRegister(0x80301714, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                case "1":
                    SetScaRegister("r_red_u1", Convert.ToUInt32(txtTemp.Text, 16));//SerialSend.WriteRegister(0x80301715, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                case "2":
                    SetScaRegister("r_red_v", Convert.ToUInt32(txtTemp.Text, 16));//SerialSend.WriteRegister(0x80301713, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                case "3":
                    SetScaRegister("r_blue_u", Convert.ToUInt32(txtTemp.Text, 16));// SerialSend.WriteRegister(0x80301716, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                case "4":
                    SetScaRegister("r_blue_v0", Convert.ToUInt32(txtTemp.Text, 16));//SerialSend.WriteRegister(0x80301717, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                case "5":
                    SetScaRegister("r_blue_v1", Convert.ToUInt32(txtTemp.Text, 16));//SerialSend.WriteRegister(0x80301718, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                case "6":
                    SetScaRegister("r_green_u", Convert.ToUInt32(txtTemp.Text, 16));//SerialSend.WriteRegister(0x80301719, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                case "7":
                    SetScaRegister("r_green_v", Convert.ToUInt32(txtTemp.Text, 16));//SerialSend.WriteRegister(0x8030171a, Convert.ToUInt32(txtTemp.Text, 16), sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void r_red_u0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                r_red_u0_Leave(sender, null);
            }
        }
    }
}
